import {getServeUrl} from '../../index.js';
import {ScenicArea, CommonPoi, IconLegend} from './view'

function getIconLegendList(scenicId, callback) {
    wx.request({
      url: getServeUrl() + '/api/samap/legends/' + scenicId + '/',
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        let legends = [];
        for (let i=0; i<res.data.length; ++i) {
          let lgd = new IconLegend(res.data[i]);
          legends.push(lgd);
        }
        if (callback != null) callback(legends); 
        // console.log(legends)
      }
    })
  }

function getScenicArea(scenicId, callback, showLoading) {
  if (showLoading) {
    wx.showLoading({
        title: '加载中',
      })
  }

  wx.request({
    url: getServeUrl() + '/api/samap/scenic_area/' + scenicId + '/',
    data: {
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success (res) {
      let scenicArea = new ScenicArea(res.data);
      if (callback != null) callback(scenicArea); 
    //   console.log(scenicArea)
    },
    complete (res) {
        if (showLoading) {
            wx.hideLoading()
        }
    }
  })
}

function getTypePoiList(scenicId, poiTypeId, callback) {
  wx.request({
    url: getServeUrl() + '/api/samap/pois/' + scenicId + '/' + poiTypeId + '/',
    data: {
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success (res) {
      let poiList = [];
      for (let i=0; i<res.data.length; ++i) {
        let poi = new CommonPoi(res.data[i]);
        poiList.push(poi);
      }
      if (callback != null) callback(poiList); 
    //   console.log(poiList)
    }
  })
}

function getScenicAreaChildren(scenicId, scenictype, callback) {
  wx.request({
    url: getServeUrl() + '/api/samap/scenic_area_children/' + scenicId + '/',
    data: {
      scenictype: scenictype,
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success (res) {
      let scenicList = [];
      for (let i=0; i<res.data.length; ++i) {
        let scenic = new ScenicArea(res.data[i]);
        scenicList.push(scenic);
      }
      if (callback != null) callback(scenicList); 
    //   console.log(scenicList)
    }
  })
}

function getScenicAreaDetail(scenic, callback) {  
  wx.request({
    url: getServeUrl() + '/api/samap/scenic_area/' + scenic.id + '/',
    data: {
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success (res) {
      scenic.init(res.data);
      scenic.detailLoaded = true;
      if (callback != null) callback(scenic); 
    //   console.log(scenic)
    }
  })
}

function getRoadDetail(scenic, road, callback) {  
    wx.request({
      url: getServeUrl() + '/api/samap/road/' + road.id + '/',
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        road.init(res.data);
        road.detailLoaded = true;
        if (callback != null) callback(road); 
      //   console.log(scenic)
      }
    })
  }

  function postScenicList(scenicids, callback) {
    let scenicidsStr = ''
    for (let i=0; i<scenicids.length - 1; ++i) {
        scenicidsStr += (scenicids[i] + ',')
    }
    scenicidsStr += scenicids[scenicids.length - 1]
    wx.request({
        url: getServeUrl() + '/api/samap/scenic_list/',
        method: 'POST',
        data: {
            scenicids: scenicidsStr
        },
        header: {
            'content-type': 'application/json' // 默认值
        },
        success (res) {
            if (callback != null) callback(res.data); 
            console.log(res)
        }
    })
}

function getCronTaskList(scenic, callback) {  
    wx.request({
      url: getServeUrl() + '/api/samap/cron_task_list/' + scenic.id + '/',
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        // road.init(res.data);
        // road.detailLoaded = true;
        if (callback != null) callback(res.data); 
      //   console.log(scenic)
      }
    })
  }

exports.getIconLegendList = getIconLegendList
exports.getScenicArea = getScenicArea
exports.getScenicAreaChildren = getScenicAreaChildren
exports.getScenicAreaDetail = getScenicAreaDetail
exports.getRoadDetail = getRoadDetail
exports.postScenicList = postScenicList
exports.getCronTaskList = getCronTaskList
global.wxRequest = exports